<template>
    <div class="register-container">
        <div class="register-card">
            <div class="register-header">
                <h1 class="app-title">镜&nbsp;莲&nbsp;华</h1>
                <div class="title-decoration"></div>
            </div>

            <el-form
                ref="registerFormRef"
                :model="registerForm"
                :rules="rules"
                label-position="top"
                class="register-form"
                status-icon
            >
                <el-form-item label="用户名" prop="name" class="form-item">
                    <el-input
                        v-model="registerForm.name"
                        placeholder="请输入3-10位用户名"
                        clearable
                        prefix-icon="User"
                        class="custom-input"
                    />
                </el-form-item>

                <el-form-item label="密码" prop="password" class="form-item">
                    <el-input
                        v-model="registerForm.password"
                        type="password"
                        placeholder="请输入6-18位密码"
                        show-password
                        prefix-icon="Lock"
                        class="custom-input"
                    />
                </el-form-item>

                <el-form-item label="确认密码" prop="confirmPassword" class="form-item">
                    <el-input
                        v-model="registerForm.confirmPassword"
                        type="password"
                        placeholder="请再次输入密码"
                        show-password
                        prefix-icon="Lock"
                        class="custom-input"
                    />
                </el-form-item>

                <el-button
                    type="primary"
                    @click="submitRegisterForm"
                    class="submit-btn"
                    :loading="loading"
                >
                    立即注册
                </el-button>

                <div class="login-link">
                    <span>已有账号？</span>
                    <el-button type="primary" link @click="toLogin">直接登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>


<script>
import { ElNotification } from 'element-plus';
import { insertUser } from '@/api/user.js';
import router from '@/router';

export default {
  data() {
    return {
      formSize: 'default',
      registerForm: {
        name: '',
        password: '',
        confirmPassword: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名长度需在3-10之间', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 18, message: '密码长度需在6-18之间', trigger: 'blur' },
        ],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              if (value !== this.registerForm.password) {
                callback(new Error('两次输入的密码不一致'));
              } else {
                callback();
              }
            },
            trigger: 'blur',
          },
        ],
      },
    };
  },
  methods: {
    showNotification(message, type) {
      ElNotification({
        title: type === 'success' ? '注册成功' : '注册失败',
        message,
        type,
        position: 'top-right',
        duration: 3000,
      });
    },
    async submitRegisterForm() {
      try {
        await this.$refs.registerFormRef.validate();

        await insertUser({
          userName: this.registerForm.name,
          password: this.registerForm.password,
        });

        this.showNotification('恭喜您，注册成功！', 'success');
        this.resetRegisterForm();
        router.push('./login');
      } catch (error) {
        console.error('注册失败:', error);
        this.showNotification('注册失败，请检查输入或稍后重试', 'error');
      }
    },
    resetRegisterForm() {
      this.$refs.registerFormRef.resetFields();
    },
    toLogin() {
      router.push('./login');
    },
  },
};
</script>
<style scoped>
.register-container {
    display: flex;
    justify-content: center;
    align-items: center;
    position: fixed; /* 使用fixed定位确保不出现滚动条 */
    top: 0;
    left: 0;
    right: 0;
    bottom: 0; /* 四个方向都固定为0，填满整个视口 */
    height: 100vh; /* 使用视口高度单位 */
    width: 100vw; /* 使用视口宽度单位 */
    background: linear-gradient(rgba(255, 255, 255, 0.5), rgba(0, 0, 0, 0.5)),
    url('@/assets/img/threeLuoLi.jpg') no-repeat center center fixed;
    background-size: cover;
    padding: 20px; /* 减少内边距 */
    overflow: hidden; /* 隐藏任何可能的溢出内容 */
    box-sizing: border-box; /* 确保padding包含在尺寸内 */
}

.register-card {
    width: 100%;
    max-width: 480px;
    padding: 40px;
    background: rgba(255, 255, 255, 0.76);
    border-radius: 16px;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(8px);
    animation: fadeInUp 0.6s ease-out;
}

.register-header {
    text-align: center;
    margin-bottom: 30px;
}

.app-title {
    color: #79bbff;
    font-family: '华文行楷', serif;
    font-size: 2.8rem;
    margin: 0;
    text-shadow: 1px 1px 4px rgba(0, 0, 0, 0.1);
    letter-spacing: 3px;
}

.title-decoration {
    height: 4px;
    width: 100px;
    background: linear-gradient(to right, #79bbff, #409eff);
    margin: 12px auto;
    border-radius: 2px;
    opacity: 0.8;
}

.welcome-text {
    color: #666;
    font-size: 1rem;
    margin-top: 10px;
}

.register-form {
    width: 100%;
}

.form-item {
    margin-bottom: 22px;
}

.custom-input {
    border-radius: 8px;
}

.custom-input :deep(.el-input__wrapper) {
    box-shadow: 0 0 0 1px #dcdfe6;
    transition: all 0.3s;
}

.custom-input :deep(.el-input__wrapper:hover) {
    box-shadow: 0 0 0 1px #79bbff;
}

.custom-input :deep(.el-input__wrapper.is-focus) {
    box-shadow: 0 0 0 1px #79bbff;
}

.password-hint {
    font-size: 12px;
    color: #999;
    margin-top: 6px;
}

.submit-btn {
    width: 100%;
    height: 48px;
    font-size: 16px;
    letter-spacing: 2px;
    background: linear-gradient(to right, #79bbff, #409eff);
    border: none;
    border-radius: 8px;
    margin-top: 10px;
    transition: all 0.3s;
}

.submit-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(121, 187, 255, 0.4);
}

.login-link {
    text-align: center;
    margin-top: 20px;
    font-size: 14px;
    color: #666;
}

.login-link .el-button {
    font-size: 14px;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@media (max-width: 480px) {
    .register-card {
        padding: 30px 20px;
        width: 90%;
    }

    .app-title {
        font-size: 2.2rem;
    }

    .title-decoration {
        width: 80px;
    }
}
</style>